package org.ttitfly.zuoyue.web.attachment;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.annotation.Resource;

import org.ttitfly.zuoyue.entity.attachment.Attachment;
import org.ttitfly.zuoyue.service.attachment.AttachmentManager;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 下载
 * @author panhaichun
 *
 */
public class DownloadAction extends ActionSupport {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** 附件ID */
	private Long id;
	
	/** 要下载的附件 */
	private Attachment attachment;
	
	@Resource
	private AttachmentManager attachmentManager;
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	public String getFileName() {
		try {
			return new String(attachment.getName().getBytes(), "ISO8859-1");
		} catch (UnsupportedEncodingException e) {
			return attachment.getName();
		}
	}
	
	public Attachment getAttachment() {
		return attachment;
	}

	public void setAttachment(Attachment attachment) {
		this.attachment = attachment;
	}

	public String execute() throws IOException {
		attachment = attachmentManager.download(id);
		return "success";
	}
}
